//
//  TabViewController.swift
//  ios_swift_demo1
//
//  Created by liutao on 2020/7/10.
//  Copyright © 2020 lt. All rights reserved.
//

import UIKit

class TabBarViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tabBar.isTranslucent = false  //设置顶部导航栏的半透明
        setupLayout()
        // Do any additional setup after loading the view.
    }
    
    func setupLayout() {
        // 1.首页
        let onePageVC = HomeTabViewController()
        addChildController(onePageVC,
                           title: "首页",
                           image: UIImage(named: "tab_home"),
                           selectedImage: UIImage(named: "tab_home_S"))
        
        
        // 2.分类
        let classVC = HomeTabViewController()
        addChildController(classVC,
                           title: "分类",
                           image: UIImage(named: "tab_class"),
                           selectedImage: UIImage(named: "tab_class_S"))
        
        
        // 3.书架
        let bookVC = BookController()
        addChildController(bookVC,
                           title: "书架",
                           image: UIImage(named: "tab_book"),
                           selectedImage: UIImage(named: "tab_book_S"))
        
        
        // 4.我的
        let mineVC = MineViewController()
        addChildController(mineVC,
                           title: "我的",
                           image: UIImage(named: "tab_mine"),
                           selectedImage: UIImage(named: "tab_mine_S"))
        
        
    }
    
    
    //添加子控制器
    
    func addChildController(_ childController : UIViewController ,title : String?, image:UIImage? ,selectedImage:UIImage?){
        childController.title = title
        childController.tabBarItem = UITabBarItem(title: nil,
                                                  image: image?.withRenderingMode(.alwaysOriginal),
                                                  selectedImage: selectedImage?.withRenderingMode(.alwaysOriginal))
        if UIDevice.current.userInterfaceIdiom == .phone {
            childController.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
        
        }
        addChild(NaviBottomController(rootViewController: childController))
        
    }
        
}


extension TabBarViewController {
    override var preferredStatusBarStyle: UIStatusBarStyle {
        guard let select = selectedViewController else { return .lightContent }
        return select.preferredStatusBarStyle
    }
}

